//
//  main.m
//  05_homework_排序
//
//  Created by dllo on 16/7/8.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Person.h"

int main(int argc, const char * argv[]) {
   
//    一、
//    1.创建1个包含5个字符串元素的数组；
//    2.用for循环倒序输出所有元素；
//    3.用for...in完成所有字符串元素的拼接(生成一个
//      新的字符串)，并输出拼接好的字符串。
    
    //  一
    // (1)
    NSArray *fiveString = @[@"你是", @"谁", @"让我来", @"好好地", @"猜一猜"];
    // (2)
    for (NSInteger i = (fiveString.count - 1); i >= 0; i--) {
        NSLog(@"%@", fiveString[i]);
    }
    // (3)
    NSString *newString = [NSString string];
    for (NSString *string in fiveString) {
        newString = [newString stringByAppendingString:string];
        NSLog(@"%@", string);
    }
    NSLog(@"%@", newString);
    
    
//    二、
//    
//    1.创建1个包含5个person对象的数组(姓名用英文)；
//    2.用for循环找到年龄最大的Person对象；
//    3.用for...in遍历数组，把大于某个年龄的Person对象的姓名后面拼接上字符串“-                      brother”。
    // 1
    Person *per1 = [[Person alloc] init];
    per1.name = @"aaa";
    per1.age = 1;
    per1.sex = @"女";
    Person *per2 = [[Person alloc] init];
    per2.name = @"bbb";
    per2.age = 2;
    per2.sex = @"男";
    Person *per3 = [[Person alloc] init];
    per3.name = @"ccc";
    per3.age = 3;
    per3.sex = @"女";
    Person *per4 = [[Person alloc] init];
    per4.name = @"ddd";
    per4.age = 4;
    per4.sex = @"男";
    Person *per5 = [[Person alloc] init];
    per5.name = @"eee";
    per5.age = 5;
    per5.sex = @"男";
    
    NSArray *arrayOfPerson = @[per1, per2, per3, per4, per5];
    
    // 2
    NSInteger a = 0;
    Person *personOfBigAge = [[Person alloc] init];
    for (int i = 0; i < arrayOfPerson.count; i++) {
        if ([arrayOfPerson[i] age] > a) {
            personOfBigAge = arrayOfPerson[i];
            a = [arrayOfPerson[i] age];
        }
    }
    NSLog(@"年龄最大的是%@", [personOfBigAge name]);
    
    // 3
    for (Person *OnePerson in arrayOfPerson) {
        if (OnePerson.age > 3) {
            NSString *stringOfBrother = [OnePerson.name stringByAppendingString:@"-brother"];
            OnePerson.name = stringOfBrother;
        }
    }
    for (Person *person in arrayOfPerson) {
        NSLog(@"%@", person.name);
    }
 
//    三、
//   
//    1.使用第二题目中生成的数组；
//    2.使用sortUsingSelector: 分别按年龄，姓名，性别进行排序，输出排序结果；
//    3.使用NSSortDescriptor，按姓名进行排序。
  
    // 2
    NSArray *arrayOfAgeSort = [arrayOfPerson sortedArrayUsingSelector:@selector(compareAge:)];
    for (Person *person in arrayOfAgeSort) {
        NSLog(@"年龄:%ld姓名:%@\n", person.age, person.name);
    }
    NSArray *arrayOfNameSort = [arrayOfPerson sortedArrayUsingSelector:@selector(compareName:)];
    for (Person *person in arrayOfNameSort) {
        NSLog(@"%@", person.name);
    }
    NSArray *arrayOfSexSort = [arrayOfPerson sortedArrayUsingSelector:@selector(compareSex:)];
    for (Person *person in arrayOfSexSort) {
        NSLog(@"%@%ld%@\n", person.sex, person.age, person.name);
    }
    // 3
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:NO];
    arrayOfPerson = [arrayOfPerson sortedArrayUsingDescriptors:@[sortDescriptor]];
    for (Person *person in arrayOfPerson) {
        NSLog(@"%@,%ld,%@", person.name, person.age, person.sex);
    }

//    四、
//    
//    处理省市区文件中的area.txt
//    将省市区进行分割处理, 省里包含市, 市里包含区
//    1. 先用数组字典表示
//    2. 将数组字典的处理方式转换成数组模型(Class)
//    [p]
    NSString *sourceOfString = [NSString stringWithContentsOfFile:@"/Users/dllo/Desktop/05_homework_排序/05_homework_排序/area.txt" encoding:NSUTF8StringEncoding error:nil];
    NSLog(@"%@", sourceOfString);
    NSArray *lineArray = [sourceOfString componentsSeparatedByString:@"\n"];
    NSLog(@"%@", lineArray);
    // 创建一个可变的省数组, 用来储存每个省
    NSMutableArray *province = [NSMutableArray array];
    // 遍历每一行字符串
    for (NSString *stringOfLine in lineArray) {
        // 判断是否为省, 创建省的字典
        if (![stringOfLine hasPrefix:@" "]) {
            // 创建可变的空得省字典
            NSMutableDictionary *proDic = [NSMutableDictionary dictionary];
            // 向省字典里中添加省名键值对
            [proDic setObject:stringOfLine forKey:@"proName"];
            // 创建一个市数组
            NSMutableArray *cityArray = [NSMutableArray array];
            // 将这个市数组添加到省字典中
            [proDic setObject:cityArray forKey:@"citys"];
            // 最后将这个省字典加入到省数组中
            [province addObject:proDic];
        }else if ([stringOfLine hasPrefix:@"  "] && ![stringOfLine hasPrefix:@"   "]){
            //创建一个可变的市字典
            NSMutableDictionary *cityDictionary = [NSMutableDictionary dictionary];
            //将市名添加到市字典中
            [cityDictionary setObject:stringOfLine forKey:@"cityName"];
            //创建一个区数组
            NSMutableArray *zoneArray = [NSMutableArray array];
            //再将区数组添加到市字典中，即为该市设置区
            [cityDictionary setObject:zoneArray forKey:@"zones"];
            //再将市字典添加到市数组中
            //1、首先要找到当前所在的省的字典，通过省数组来获得当前所在的省
            NSMutableDictionary *proDic = [province lastObject];
            //2、通过省字典的市数组的key值，将市字典添加到市数组中去，将该市添加到该省对应的市数组中去
            [[proDic objectForKey:@"citys"] addObject:cityDictionary];
        }else{
            //如果不是省，不是市，就是区
            //创建一个可变的区字典
            NSMutableDictionary *zoneDictionary = [NSMutableDictionary dictionary];
            //将区名添加到区字典中，设置区名
            [zoneDictionary setObject:stringOfLine forKey:@"zoneName"];
            //区下面没有数组了，不用再创建数组了，直接将区字典添加到对应的市的字典的区数组中去
            //1、先找到当前所在省的字典
            NSMutableDictionary *proDic = [province lastObject];
            //2、再找到省字典中的市数组中对应的市
            NSMutableDictionary *cityDic = [[proDic valueForKey:@"citys"] lastObject];
            //3、找到区所在市的区数组
            NSMutableArray *zoneArray = [cityDic objectForKey:@"zones"];
            //4、再将区字典存放到对应的市数组中去
            [zoneArray addObject:zoneDictionary];
        }
    }
    //打印出省市区，遍历省数组
    for (NSMutableDictionary *proDic in province) {
        NSLog(@"pro:%@", [proDic objectForKey:@"proName"]);
        //遍历该省的市数组
        for (NSMutableDictionary *cityDic in [proDic objectForKey:@"citys"]) {
            NSLog(@"city:%@", [cityDic objectForKey:@"cityName"]);
            //遍历该市的区数组
            for (NSMutableDictionary *zoneDic in [cityDic objectForKey:@"zones"]) {
                NSLog(@"zone:%@", [zoneDic objectForKey:@"zoneName"]);
            }
        }
    }
    
    
    
    
    
//    五、拓展
//    
//    上述作业做完, 处理Location1.txt
//    要求同第四题。自己寻找规律
  
     NSString *stringFiveTitle = [NSString stringWithContentsOfFile:@"/Users/dllo/Desktop/05_homework_排序/05_homework_排序/Location1.txt" encoding:NSUTF8StringEncoding error:nil];
    NSLog(@"%@", stringFiveTitle);
    
    
    
    
    

    return 0;
}
